package com.boardgame.gwt.service.operate.action.Initial;

import com.boardgame.common.entity.ResponseInfo;
import com.boardgame.gwt.data.GwtGameBoard;
import com.boardgame.gwt.data.GwtPlayerInfo;
import com.boardgame.gwt.data.space.GwtSpace;
import com.boardgame.gwt.data.space.GwtSpaceTeepee;
import com.boardgame.gwt.data.tile.teepee.GwtSpaceTileTeepee;
import com.boardgame.gwt.finalinfo.SyntaxMatchFinal;
import com.boardgame.gwt.service.operate.SyntaxRequest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 印第安帐篷交易
 */
public class IndiansTradeAction extends InitialAction{
    @Override
    public Pattern getPattern() {
        return Pattern.compile(SyntaxMatchFinal.INITIAL_IndiansTrade_ACTION);
    }

    @Override
    public ResponseInfo action(GwtGameBoard gwtGameBoard, SyntaxRequest syntaxRequest, Matcher matcher) {
        super.action(gwtGameBoard, syntaxRequest, matcher);
        //获取要拿取的行动格子
        GwtSpace gwtSpace = super.getGwtSplace(matcher);
        //判断是不是帐篷格子
        if (gwtSpace instanceof GwtSpaceTeepee){
            //其次，格子必须有帐篷
            if (gwtSpace.getGwtSpaceTile()==null){
                return ResponseInfo.Failed("你选择的位置为空!");
            }
            GwtPlayerInfo gwtPlayerInfo = super.gwtGameBoard.getCurrentPlayer();
            //如果需要支付，判断是否足够
            if (gwtSpace.getCost()<0){
                if (gwtPlayerInfo.getCoins()<Math.abs(gwtSpace.getCost())){
                    return ResponseInfo.Failed("你的金钱不能支付购买!");
                }
            }
            else
            {
                //加上拿的钱
                gwtPlayerInfo.setCoins(gwtPlayerInfo.getCoins()+gwtSpace.getCost());
                //玩家拿取的
                GwtSpaceTileTeepee gwtSpaceTileTent = (GwtSpaceTileTeepee)gwtSpace.getGwtSpaceTile();
                gwtPlayerInfo.getListTileTent().add(gwtSpaceTileTent);
                //删除当前格子的帐篷
                gwtSpace.setGwtSpaceTile(null);
            }

        }
        else{
            return ResponseInfo.Failed("你选择的格子不是帐篷!");
        }
        return ResponseInfo.Success(null);
    }

    public static IndiansTradeAction create(){
        return new IndiansTradeAction();
    }
}
